package com.yuwubao.uav.tool.service.impl;

import com.yuwubao.framework.bean.PageData;
import com.yuwubao.uav.tool.mapper.ProfessorMapper;
import com.yuwubao.uav.tool.service.ProfessorService;
import com.yuwubao.uav.tool.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class ProfessorServiceImpl implements ProfessorService {

    private static final Logger logger = LogManager.getLogger(ProfessorServiceImpl.class.getName());

    @Resource
    private ProfessorMapper professorMapper;

    @Override
    public void deleteProfessor(PageData pageData) throws Exception {
        try{
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");
            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    professorMapper.deleteProfessor(pageData);

                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Transactional
    @Override
    public void updateProfessor(PageData pageData) throws Exception {
        try{
            professorMapper.updateProfessor(pageData);

            String fields = pageData.get("fieldIds") == null ? "" : pageData.getString("fieldIds");
            if (StringUtil.isNotNvlOrBlank(fields)) {

                professorMapper.deleteProfessorResearchFieldRelation(pageData);

                String[] idArr = fields.split(",");
                if (idArr != null && idArr.length>0) {
                    for (String fieldId : idArr) {
                        pageData.put("fieldId",fieldId);
                        professorMapper.addProfessorResearchFieldRelation(pageData);
                    }
                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void disableOrEnableProfessor(PageData pageData) throws Exception {
        try{
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");
            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    professorMapper.disableOrEnableProfessor(pageData);

                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void disableOrEnableResearchField(PageData pageData) throws Exception {
        try{
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");
            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    professorMapper.disableOrEnableResearchField(pageData);

                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Transactional
    @Override
    public void addProfessor(PageData pageData) throws Exception {
        try{
            professorMapper.addProfessor(pageData);

            String fields = pageData.get("fieldIds") == null ? "" : pageData.getString("fieldIds");
            if (StringUtil.isNotNvlOrBlank(fields)) {
                String[] idArr = fields.split(",");
                if (idArr != null && idArr.length>0) {
                    for (String fieldId : idArr) {
                        pageData.put("fieldId",fieldId);
                        professorMapper.addProfessorResearchFieldRelation(pageData);
                    }
                }
            }

        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Override
    public List<PageData> listProfessor(PageData pageData) throws Exception {
        try{
            List<PageData> list =professorMapper.listProfessor(pageData);

            if (list != null && list.size() >0) {
                for (PageData pd : list) {
                    pd.put("research_field_list",professorMapper.listProfessorResearchField(pd));
                }

            }
            return list;
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listProfessorCount(PageData pageData) throws Exception {
        try{
            return professorMapper.listProfessorCount(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }

    @Override
    public void deleteResearchField(PageData pageData) throws Exception {
        try{

            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");

            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("idArr",idArr);
                    professorMapper.deleteResearchField(pageData);

                }
            }


        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Override
    public void updateResearchField(PageData pageData) throws Exception {
        try{
            professorMapper.updateResearchField(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Override
    public void addResearchField(PageData pageData) throws Exception {
        try{
            String names = pageData.get("names") == null ? "" : pageData.getString("names");
            if (StringUtil.isNotNvlOrBlank(names)) {
                String[] nameArr = names.split(",");
                if (nameArr != null && nameArr.length>0) {
                    int count = 0;
                    for (String name : nameArr) {
                        pageData.put("name",name);
                        count = professorMapper.getResearchFieldByName(pageData);
                        if (count == 0) {
                            professorMapper.addResearchField(pageData);
                        }
                    }
                }
            }
        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Override
    public List<PageData> listResearchField(PageData pageData) throws Exception {
        try{
            return professorMapper.listResearchField(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public List<PageData> listResearchFieldAll(PageData pageData) throws Exception {
        try{
            return professorMapper.listResearchFieldAll(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listResearchFieldCount(PageData pageData) throws Exception {
        try{
            return professorMapper.listResearchFieldCount(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }
    @Override
    public int getResearchFieldByName(PageData pageData) throws Exception {
        try{
            return professorMapper.getResearchFieldByName(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }

}
